﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace 作业1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadUserDirectories();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void LoadUserDirectories()
        {
            treeView1.Nodes.Clear();
            string userFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            TreeNode rootNode = new TreeNode(userFolder) { Tag = userFolder };
            treeView1.Nodes.Add(rootNode);
            LoadSubDirectories(rootNode);
            rootNode.Expand();
        }
        private void LoadSubDirectories(TreeNode node)
        {
            string path = node.Tag.ToString();
            try
            {
                foreach (string dir in Directory.GetDirectories(path))
                {
                    TreeNode subNode = new TreeNode(Path.GetFileName(dir)) { Tag = dir };
                    node.Nodes.Add(subNode);
                    subNode.Nodes.Add("加载中..."); // 占位符
                }
            }
            catch { }
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            LoadUserDirectories();
        }
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = e.Node;
            selectedNode.Nodes.Clear(); // 清除“加载中...”占位符
            LoadSubDirectories(selectedNode);

            listView1.Items.Clear();
            string path = selectedNode.Tag.ToString();

            try
            {
                foreach (string dir in Directory.GetDirectories(path))
                {
                    listView1.Items.Add(new ListViewItem(new string[] { Path.GetFileName(dir), "文件夹" }));
                }
                foreach (string file in Directory.GetFiles(path))
                {
                    listView1.Items.Add(new ListViewItem(new string[] { Path.GetFileName(file), "文件" }));
                }
            }
            catch { }
        }
        private void ShowXmlContent(string filePath)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);

                string content = "";
                foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                {
                    content += $"元素名: {node.Name}\n";
                    if (node.Attributes != null)
                    {
                        foreach (XmlAttribute attr in node.Attributes)
                        {
                            content += $"  属性: {attr.Name} = {attr.Value}\n";
                        }
                    }
                    content += $"  内容: {node.InnerText}\n\n";
                }

                MessageBox.Show(content, "XML 内容", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取 XML 错误: " + ex.Message);
            }
        }
        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;

            var selected = listView1.SelectedItems[0];
            if (selected.SubItems[1].Text == "文件")
            {
                TreeNode node = treeView1.SelectedNode;
                string folderPath = node.Tag.ToString();
                string filePath = Path.Combine(folderPath, selected.Text);

                if (Path.GetExtension(filePath).ToLower() == ".xml")
                {
                    ShowXmlContent(filePath);
                }
            }
        }
    }
}
